package com.ls.lserp.modules.setting.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.setting.dao.QueryCompanyDao;
import com.ls.lserp.modules.setting.entity.CompanyList;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.DictUtils;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
@Service
@Transactional(readOnly = false)
public class QueryCompanyService extends CrudService<QueryCompanyDao,CompanyList>{
    
    @Autowired
    private QueryCompanyDao queryCompanyDao ;

    /**
	 * 查询父项
	 * @param page
	 * @param user
	 * @return
	 */
    public Page<CompanyList> findCompanyList(Page<CompanyList> page, CompanyList companyList) {
        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
        // 设置分页参数
    	companyList.setPage(page);
        // 执行分页查询
        page.setList(queryCompanyDao.findList(companyList));
        return page;
    }
    /**
	 * ID查询一条数据
	 * 
	 * @param user
	 * @return
	 */
    public CompanyList findACompany(CompanyList companyList){
    	
    	return queryCompanyDao.get(companyList);
    }
       
       
    
    public void insertCompany(CompanyList companyList) throws SQLException{
    	
    	//国家取得
        Dict dictCountry=DictUtils.getDictEntity(companyList.getCompanyCountry());
		//城市取得
        Dict dictCity=DictUtils.getDictEntity(companyList.getCompanyCity());
//		//客户类别取得
//        Dict dictCategory=DictUtils.getDictEntity(companyList.getCustomerCategoryCD());
//		//客户级别取得
//        Dict dictLevel=DictUtils.getDictEntity(companyList.getCustomerLevelCD());
        String companyShortName = companyList.getCompanyShortName();
        
        StringBuilder conCode = new StringBuilder();
        conCode.append(dictCountry.getItemShortNameEn());
        conCode.append(dictCity.getItemShortNameEn());
//        conCode.append(dictCategory.getItemShortNameEn());
        conCode.append(DateUtils.formatDate(new Date(), "yyyy"));
//        conCode.append(dictLevel.getItemShortNameEn());
        conCode.append(companyShortName);
        // 店铺编号
        Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_company", 4);
        companyList.setCompanyNo(sequence.getReturnCode().toUpperCase());
    	
    	companyList.setMyType((long) 9);
    	queryCompanyDao.insert(companyList);
    	if (companyList.getCompanyAccountList()!=null) {
    	queryCompanyDao.bankInsert(companyList);
    	}
    	
    }
    /**
	 * 更新一条数据
	 * 
	 * @param user
	 * @return
	 */
    public void updateCompany(CompanyList companyList) throws SQLException{
    	
    	queryCompanyDao.update(companyList);
    	queryCompanyDao.delete(companyList);
    	if (companyList.getCompanyAccountList()!=null) {
		
		queryCompanyDao.bankInsert(companyList);	
    	}
    	
    }
    
    /**
	 * 查询公司银行账户
	 * 
	 * @param user
	 * @return
	 */
    public List<CompanyList> findBankAccount(CompanyList companyList){

    	return queryCompanyDao.findBankList(companyList);
    	
    }
    
    
    
    
    
}    